<template>
  <div class="login-box">
    <div class="form-box">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span class="kpname">黑马面经运行后台</span>
        </div>
        <el-form ref="form" :rules="rules" :model="form">
          <el-form-item label="用户名" prop="username">
            <el-input
              @keyup.enter.native="$refs.pwd.focus()"
              v-model="form.username"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              ref="pwd"
              v-model="form.password"
              placeholder="请输入密码"
              show-password
              @keyup.enter.native="login"
            ></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="login" :loading="flag">
              登录
            </el-button>
            <el-button @click="reset"> 重置 </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/login.js'

export default {
  name: 'login-page',
  components: {},
  props: {},
  data() {
    return {
      flag: false,
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 5,
            max: 11,
            message: '用户名长度在 5 到 11 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^[a-z]{5,11}$/,
            message: '密码必须是字母长度在 5 到 11个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    async login() {
      try {
        await this.$refs.form.validate()
        this.flag = true
        const { data } = await login(this.form)
        // console.log(data)
        this.$store.commit('user/setToken', data.token)
        this.flag = false
        this.$message.success('登录成功')
        this.$router.push('/')
      } catch (error) {
        console.log(error)
        if (error.response && error.response.status === 400) {
          this.$message.error('管理员不存在或者用户名密码错误')
        } else {
          this.$message.error('表单验证失败！')
        }
      }
    },
    reset() {
      this.$refs.form.resetFields()
      this.flag = false
    }
  }
}
</script>

<style scoped lang="scss">
.login-box {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/login-bg.svg') no-repeat center center/cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .form-box {
    width: 420px;
    border-radius: 5px;
    ::v-deep .el-card__header {
      text-align: center;

      background-color: rgb(131, 142, 237);
      color: rgb(255, 255, 255);
    }
    .btns {
      text-align: center;
      .el-button:first-child {
        background-color: rgb(131, 142, 237);
      }
    }
  }
}
</style>
